package com.spring.demo12.eg2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;

import java.util.List;

/**
 * @author vhans
 */
@ComponentScan
@Configuration
public class MainConfig1 {

    @Bean
    @Autowired
    public ApplicationEventMulticaster applicationEventMulticaster(List<ApplicationListener> applicationListeners) {
        SimpleApplicationEventMulticaster simpleApplicationEventMulticaster = new SimpleApplicationEventMulticaster();
        applicationListeners.forEach(simpleApplicationEventMulticaster::addApplicationListener);
        return simpleApplicationEventMulticaster;
    }

    @Bean
    public OrderCreateService orderCreateService(ApplicationEventMulticaster applicationEventMulticaster) {
        OrderCreateService orderCreateService = new OrderCreateService();
        orderCreateService.setEventMulticaster(applicationEventMulticaster);
        return orderCreateService;
    }
}
